﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ActionRepository.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ActionRepository type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Utilities
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Utils;

    public class ActionRepository
    {
        /// <summary>
        /// Adds a property to the web property bag
        /// </summary>
        /// <param name="web" />
        /// <param name="propertyName" />
        /// <param name="propertyValue" />
        /// <typeparam name="T" />
        public static void AddProperty<T>(SPWeb web, string propertyName, T propertyValue) 
        {
            ValidationHelper.VerifyObjectArgument(web, "web");
            ValidationHelper.VerifyStringArgument(propertyName, "propertyName");
                        
            string serialisedPropValue = SerialisationHelper.Serialize<T>(propertyValue);
            if (web.Properties.ContainsKey(propertyName))
                web.Properties.Remove(propertyName);
            web.Properties.Add(propertyName, serialisedPropValue);
            web.Properties.Update();
        }

        /// <summary>
        /// Adds a property to the Farm Hierarchical storage. It requires Farm administration permissions
        /// </summary>
        /// <param name="propertyName" />
        /// <param name="propertyValue" />
        /// <param name="farm" />
        /// <typeparam name="T" />
        public static T AddProperty<T>(SPFarm farm, string propertyName, T propertyValue) where T : SPPersistedObject, new()
        {
            ValidationHelper.VerifyObjectArgument(farm, "farm");
            ValidationHelper.VerifyStringArgument(propertyName, "propertyName");

            return farm.GetChild<T>(propertyName);
        }


    }
}